﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace Acms.AcmsControls.ContentControls.FrontEnd
{
    /// <summary>
    /// renders the current post or containg posts thumnail if one is present
    /// </summary>
    public class PostThumbnail : BasePostPropertyControl
    {
        public override void DataBind()
        {
            var post = FindCurrentPost();
            if (post != null)
            {
                ThumbnailId = post.ThumbnailId;
                ToolTip = post.DisplayName;
                Url = post.Url;
            }
            else
            {
                ThumbnailId = ((FindCurrentIndex()+1) % 6) * -1;
                ToolTip = "Thumbnail Here";
                Url = "#";
            }

            base.DataBind();
        }

        public int? ThumbnailId { get; set; }
        public string Url { get; set; }
        /// <summary>
        /// Class to render - if hyperlink is true then renders class on hyperlink else render on image
        /// </summary>
        public string CssClass { get; set; }
        public string ToolTip { get; set; }
        /// <summary>
        /// Wheter to render link as well - Default is false
        /// </summary>
        public bool HyperLink { get; set; }
        /// <summary>
        /// If hyperlink = true add this property set - will target attribuite
        /// </summary>
        public string Target { get; set; }
        /// <summary>
        /// Max Height to render the thumbnail
        /// </summary>
        public int RenderHeight { get; set; }
        /// <summary>
        /// Max Width to render the thumbnila
        /// </summary>
        public int RenderWidth { get; set; }

        protected override object SaveViewState()
        {
            ViewState["ThumbnailId"] = ThumbnailId;
            ViewState["Url"] = Url;
            ViewState["ToolTip"] = ToolTip;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            ThumbnailId = ViewState["ThumbnailId"] as int?;
            Url = ViewState["Url"] as string;
            ToolTip = ViewState["ToolTip"] as string;
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (ThumbnailId != null && ThumbnailId.Value != 0)
            {
                // because i was right. Render css class on outter tag, either img or a
                if (!string.IsNullOrEmpty(this.CssClass))
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass);
                }

                if (HyperLink)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
                    writer.AddAttribute(HtmlTextWriterAttribute.Href, Page.ResolveUrl(this.Url));
                    
                    if (!string.IsNullOrEmpty(this.Target))
                    {
                        writer.AddAttribute(HtmlTextWriterAttribute.Target, this.Target);
                    }
                    writer.AddAttribute(HtmlTextWriterAttribute.Alt, this.ToolTip);
                    writer.RenderBeginTag(HtmlTextWriterTag.A);
                }
                
                writer.AddAttribute(HtmlTextWriterAttribute.Src, Page.ResolveUrl(string.Format("~/File.aspx?Id={0}&width={1}&height={2}", ThumbnailId.Value, RenderWidth, RenderHeight)));
                writer.AddAttribute(HtmlTextWriterAttribute.Id, string.Concat(this.ClientID, "_Img"));
                writer.AddAttribute(HtmlTextWriterAttribute.Alt, this.ToolTip);
                writer.RenderBeginTag(HtmlTextWriterTag.Img);
                writer.RenderEndTag();
                if (HyperLink)
                {
                    writer.RenderEndTag();
                }
            }
        }
    }
}
